import os
import pandas as pd
from utils.tools import to_pdtime_ms, to_pdtime_str, get_mysql

columns = ['VERSION', 'PASSTIME', 'CARSTATE', 'CARPLATE', 'PLATETYPE', 'SPEED', 'PLATECOLOR', 'LOCATIONID'
    , 'DEVICEID', 'DRIVEWAY', 'DRIVEDIR', 'CAPTUREDIR', 'CARCOLOR', 'CARBRAND', 'CARBRANDZW', 'TGSID', 'PLATECOORD'
    , 'CABCOORD', 'IMGID1', 'IMGID2', 'IMGID3']

columns_use = ['PASSTIME', 'CARPLATE', 'SPEED', 'DRIVEWAY', 'DRIVEDIR', 'CAPTUREDIR', 'PLATETYPE', 'TGSID']

def read_data(file):
    list = []
    with open(file ,"r",encoding="utf-8") as lines:
        for line in lines.read().strip().split("\n"):
            data = []
            if line.startswith("VERSION=1.0"):
                field = lisocketne.split("\t")
                for dic in field:
                    value = dic.split("=")
                    if len(value)>0:
                        # print(value[0],value[1])
                        data.append(value[1])
                        # items[value[0]] = value[1]
                list.append(data)
    df = pd.DataFrame(list,columns=columns)
    return df

def file_to_df(file):
    print('converting file:%r(%r MB)' % (file, (os.path.getsize(file) / (1024 * 1024))))
    df_org = read_data(file)
    df = df_org[columns_use].copy()
    # for k in df.columns:
    #     _, df[k] = df[k].astype(str).str.split('=', 1).str

    df['PASSTIME'] = to_pdtime_str(df['PASSTIME'].str[0:19],tz=False)
    df = df[df['PASSTIME'].notnull()]

    # df = df.set_index('PASSTIME')
    #   请记得，一定要有赋值操作，否则更改的就没有意义

    df['DRIVEDIR'] = df['DRIVEDIR'].astype('int32', errors='ignore')
    df['DRIVEWAY'] = df['DRIVEWAY'].astype('int32', errors='ignore')

    # if (not pd.api.types.is_integer_dtype(df['DRIVEDIR'])):
    #     df['DRIVEDIR'] = pd.to_numeric(df['DRIVEDIR'], errors='coerce')
    #     df = df[df['DRIVEDIR'].notnull()].copy()
    #     df['DRIVEDIR'] = df['DRIVEDIR'].astype('int32')
    #
    # if (not pd.api.types.is_integer_dtype(df['DRIVEWAY'])):
    #     df['DRIVEWAY'] = pd.to_numeric(df['DRIVEWAY'], errors='coerce')
    #     df = df[df['DRIVEWAY'].notnull()].copy()
    #     df['DRIVEWAY'] = df['DRIVEWAY'].astype('int32')
    df.save('t_passcar',mysql)
    print(f'save {file}')


def read_file():
    dir = r'E:\EHL\CODE\python\项目\新乡\pr'
    for root, dirs, files in os.walk(dir):
        for file in files:
            dir = os.path.join(root,file)
            file_to_df(dir)



mysql = get_mysql('tsce','ehl1234','10.2.111.57','tsce',4000)


if __name__ == '__main__':
    print('ok')
    read_file()
    # file_to_df(r"E:\EHL\CODE\python\项目\新乡\pr\20181022\41.111.32.20-tvc-2018-10-22-00")
